﻿    //----------------------------------------------------------------------
    //???
    public static bool FindWordInFile(string word, string whichfile)
    {
        StreamReader FileSearch = new StreamReader(whichfile);

        word = word.ToUpper();
        
        //See if any matches
        while (!FileSearch.EndOfStream)
        {
            if (word.EndsWith(FileSearch.ReadLine()))
            {
                FileSearch.Close();
                return true;
            }
        }

        //Did not find anything
        FileSearch.Close();
        return false;
    }

    //-------------------------------
    public static void TranslateAnswer(string input)
    {
        if (!KidAnswers(input))
        {
            if (FindWordInFile(input, "ans-positive.dat"))
                KidSaysRandomStatement("KID-unknown.dat");
            else if (FindWordInFile(input, "ans-negative.dat"))
                KidSaysRandomStatement("KID-targetnegative.dat");
            else KidSaysRandomStatement("KID-unknown.dat");
        }
    //----------------------------------------------------------------------
    //This function checks if the user wrote a positive or negative statment
    public static bool CheckAnswers()
    {
        string input;
        StreamReader PossibleAnswers = new StreamReader("ans-positive.dat");
        
        input = Console.ReadLine();
        input = input.ToUpper();

        //See if any positive answers fit
        while (!PossibleAnswers.EndOfStream)
        {
            if (PossibleAnswers.ReadLine() == input) 
            {
                PossibleAnswers.Close();
                return true;
            }
        }

        //Now check all negative answers
        PossibleAnswers = new StreamReader("ans-negative.dat");
        while (!PossibleAnswers.EndOfStream)
        {
            if (PossibleAnswers.ReadLine() == input) 
            {
                PossibleAnswers.Close();
                return false;
            }
        }

        //Something went wrong...
        PossibleAnswers.Close();
        return false;
    }

    //----------------------------------------------------------------------
    //This function counts the number of lines in a file.
    public static int GetFileLenght(StreamReader whichfile)
    {
        int linecount = 0;
        while (!whichfile.EndOfStream)
        {
            whichfile.ReadLine();
            linecount++;
        }
        whichfile.Close();
        return linecount;
    }

    //----------------------------------------------------------------------
    //This function makes KIDJ pick a random answer from a topic
    public static bool KidAnswers(string findtopic)
    {
        StreamReader ReadFile = new StreamReader("KID-biglist.dat");
        Random rand = new Random();
        int randomanswer = 0;
        int i = 0;
        string topic;
        bool topicfound = false;

        //Find the topic
        findtopic = findtopic.ToUpper();
        while(!topicfound  && !ReadFile.EndOfStream)
        {
            if(Convert.ToChar(ReadFile.Peek()) == '#')
            {
                ReadFile.Read();
                topic = ReadFile.ReadLine();
                if (topic == findtopic) topicfound = true;
            }
            else ReadFile.ReadLine();         //Skip this line
        }

        //Count the number of answers
        if (topicfound)
        {
            while (Convert.ToChar(ReadFile.Peek()) != '#')
            {
                ReadFile.ReadLine();
                randomanswer++;
            }
        }
        else return false;                        //No valid topics found

        //Reopen file reset
        ReadFile.Close();       
        ReadFile = new StreamReader("KID-biglist.dat");
        
        //Goto topic
        topicfound = false;
        while(!topicfound)
        {
            if(Convert.ToChar(ReadFile.Peek()) == '#')
            {
                ReadFile.Read();
                topic = ReadFile.ReadLine();
                if (topic == findtopic) topicfound = true;
            }
            else ReadFile.ReadLine();         //Skip this line
        }

        //Randomize the answer
        randomanswer = rand.Next(0, randomanswer);

        while (randomanswer != i)
        {
            ReadFile.ReadLine();
            i++;
        }

        KID.Say(ReadFile.ReadLine());
        Console.ForegroundColor = ConsoleColor.White;
        ReadFile.Close();
        return true;
    }

    //----------------------------------------------------------------------
    //This function makes KID say a random discussion starting statement
    public static void KidSaysRandomStatement(string whichfile)
    {
        StreamReader PossibleStatements = new StreamReader(whichfile);
        Random rand = new Random();
        int randomstatement;
        int statlenght, i = 0;
        string test;

        statlenght = GetFileLenght(PossibleStatements);
        PossibleStatements = new StreamReader(whichfile);


        randomstatement =  rand.Next(0, statlenght);

        while(randomstatement != i)
        {
            PossibleStatements.ReadLine();
            i++;
        }
        test = PossibleStatements.ReadLine();
        KID.Say(test);
    }